package cn.jhc.teachingquality.net.teaching;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jhc.teachingquality.Global;
import static cn.jhc.teachingquality.Global.*;
import cn.jhc.teachingquality.net.TeachingQualityRequest;

/**
 * 获取待审核教学日志，handleRespose中返回的List是所有待审核日志的hiddenId集合。
 * Created by luyanfei on 16-4-7.
 */
public class ListUncheckedTeachingLogsRequest extends TeachingQualityRequest<List<String>> {
    private static final String LIST_UNCHECKED_TEACHING_LOGS_URL =
            Global.URL_PREFIX + "/jcKcxxAction!listForJsSh.shtml";

    public ListUncheckedTeachingLogsRequest(Response.Listener<List<String>> listener) {
        super(Method.POST, LIST_UNCHECKED_TEACHING_LOGS_URL, listener);
    }

    @Override
    protected List<String> handleResponse(Document document) {
        List<String> hiddenIds = new ArrayList<>();
        Elements totalLogs = document.select("#listBody tbody tr");
        for(Element element : totalLogs){
            String hiddenId;
            String status;
            try {
                hiddenId = element.getElementsByAttributeValue("name", "idKeys").first().val().trim();
                status = element.getElementsByAttributeValue("name", "zt").first().text().trim();
            } catch (NullPointerException npe){
                Log.w(LOG_TAG, "Skip one tr element in unchecked teaching log lists.");
                continue;
            }
            //只保存未审状态的纪录
            if(status.contains("未审"))
                hiddenIds.add(hiddenId);
        }
        return hiddenIds;
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> map = new HashMap<>();
        //同时显示400条记录
        map.put("page.pageSize", Global.PAGE_SIZE);
        return map;
    }
}
